import { getOrderList, getOrderDetail, getByNumber, deleteOrder, cancelOrder } from '../services/order'
import { message } from 'antd'
export default {

  namespace: 'order',

  state: {
    orderList: [],
    detail: {},
    payDetail: {}
  },

  subscriptions: {
    setup({ dispatch, history }) {
    },
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const res = yield call(getOrderList, payload);
      if (res.data.code == 0) {
        yield put({ type: 'save', payload: res.data.data });
      }
    },
    *fetchDetail({ payload }, { call, put }) {
      const res = yield call(getOrderDetail, payload);
      if (res.data.code == 0) {
        yield put({ type: 'saveDetail', payload: res.data.data });
      }
    },
    *payDetail({ payload }, { call, put }) {
      const res = yield call(getByNumber, payload);
      if (res.data.code == 0) {
        yield put({ type: 'savePayDetail', payload: res.data.data });
      }
    },
    *deleteOrder({ payload }, { call, put }) {
      const res = yield call(deleteOrder, payload);
      if (res.data.code == 0) {
        const resOrder = yield call(getOrderList, payload);
        if (resOrder.data.code == 0) {
          message.destroy()
          message.success('操作成功')
          yield put({ type: 'save', payload: resOrder.data.data });
        }
      } else {
        message.destroy()
        message.error(res.data.msg)
      }
    },
    *cancelOrder({ payload }, { call, put }) {
      const res = yield call(cancelOrder, payload);
      if (res.data.code == 0) {
        const resOrder = yield call(getOrderList, payload);
        if (resOrder.data.code == 0) {
          message.destroy()
          message.success('操作成功')
          yield put({ type: 'save', payload: resOrder.data.data });
        }
      }
    },
  },

  reducers: {
    save(state, action) {
      return { ...state, orderList: action.payload };
    },
    saveDetail(state, action) {
      return { ...state, detail: action.payload };
    },
    savePayDetail(state, action) {
      return { ...state, payDetail: action.payload };
    },
  },

};
